package com.morgan.basemodule.adapter

import android.util.Log
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.morgan.basemodule.fragment.IFragNotifyChanged


/**
 *description：FragmentPagerAdapter的通用实现，结合IFragNotifyChanged，缺点fragment会一直存在于FragmentManager中，会浪费内存
 *<br>author：caowugao
 *<br>time：2021/4/9 0009 17:09
 */
open class CommonFragPagerAdapter (var list : List<Fragment>, fm : FragmentManager, behavior : Int): FragmentPagerAdapter(fm, behavior) {

    override fun getCount(): Int = list.size

    override fun getItem(position: Int): Fragment = list[position]

    override fun getItemPosition(`object`: Any): Int {
        val oldItemPos = super.getItemPosition(`object`)
        val frag = `object`
        if (frag is IFragNotifyChanged) {
            return if (frag.isChanged()) POSITION_NONE else oldItemPos
        }
        return oldItemPos
    }

}